import { getStore, removeStore, setStore } from "@/util/store";
import { getPublic, getSysInfo } from "@/api/admin/sys-public-param";
import website from "@/const/website";

const common = {
  state: {
    isCollapse: true,
    isFullScren: false,
    isShade: false,
    isPhysical: false,
    isSalon: '',
    screen: -1,
    isLock: getStore({ name: "isLock" }) || false,
    showTag: getStore({ name: "showTag" }) || true,
    showRepair: getStore({ name: "showRepair" }) || false,
    autoRepair: getStore({ name: "autoRepair" }) || false,
    showDebug: getStore({ name: "showDebug" }) || false,
    showCollapse: getStore({ name: "showCollapse" }),
    showSearch: getStore({ name: "showSearch" }),
    showLock: getStore({ name: "showLock" }),
    showFullScreen: getStore({ name: "showFullScreen" }),
    showTheme: getStore({ name: "showTheme" }),
    showColor: getStore({ name: "showColor" }) || false,
    showMenu: getStore({ name: "showMenu" }),
    theme: getStore({ name: "theme" }) || "#409EFF",
    themeName: getStore({ name: "themeName" }) || "default",
    lockPasswd: getStore({ name: "lockPasswd" }) || "",
    website: website,
    unreadMessage: 0,
    systemName: "",
    passwordLevel: 1,
    shouldEdit: getStore({ name: "shouldEdit" }) || false,
    showCheckCode: 0,
    checkCodeTip: "",
    loginInfo: {},
    loginList: [],
    showEvaluate: false,
    callbackUrl:""
  },
  getters: {
    unreadMessage: state=>  state.unreadMessage,
    isSalon: state=>  state.isSalon
  },
  actions: {
    getPublic({ state, commit }) {
      commit("SET_WEBSITE", getStore({ name: "systemName" }));
      if (state.systemName) return;
      getSysInfo("SYSTEM_NAME").then((res) => {
        const data = res.data.data;
        commit("SET_SYSTEM_NAME", data.name);
        commit("SET_WEBSITE", data.name);
        commit("SET_LOGIN_INFO", data.routes[0]);
        commit("SET_LOGIN_lIST", data.routes);
        commit("SET_PASSWORD_LEVEL", data["passwordLevel"] || 1);
        commit("SET_CHECK_CODE", data["checkCode"] || 0);
        commit("SET_CHECK_TIP", data["checkCodeTip"] || "");
      });
    },
    setLoginInfo({ commit }, data) {
      commit("SET_LOGIN_INFO", data);
    },
    setShouldEdit({ commit }, data) {
      commit("SET_SHOULD_EDIT", data);
    },
    setCallbackUrl({ commit }, data){
      commit("SET_CALLBACKURL", data);
    }
  },
  mutations: {
    SET_LOGIN_lIST: (state, data) => {
      state.loginList = data;
    },
    SET_LOGIN_INFO: (state, data) => {
      state.loginInfo = data;
    },
    SET_WEBSITE: (state, data) => {
      if (data) {
        state.website.indexTitle = data;
        state.website.infoTitle = data;
      }
    },
    SET_SYSTEM_NAME: (state, data) => {
      setStore({ name: "systemName", content: data });
      state.systemName = data;
    },
    SET_SHADE: (state, active) => {
      state.isShade = active;
    },
    SET_COLLAPSE: (state) => {
      state.isCollapse = !state.isCollapse;
    },
    SET_FULLSCREN: (state) => {
      state.isFullScren = !state.isFullScren;
    },
    SET_PHYSICAL: (state, data) => {
      state.isFullScren = !state.isFullScren;
    },
    SET_SALON: (state, data) => {
      state.isSalon = data;
    },
    SET_SHOW_COLLAPSE: (state, active) => {
      state.showCollapse = active;
      setStore({
        name: "showCollapse",
        content: state.showCollapse,
      });
    },
    SET_SHOW_MENU: (state, active) => {
      state.showMenu = active;
      setStore({
        name: "showMenu",
        content: state.showMenu,
      });
    },
    SET_SHOW_LOCK: (state, active) => {
      state.showLock = active;
      setStore({
        name: "showLock",
        content: state.showLock,
      });
    },
    SET_SHOW_SEARCH: (state, active) => {
      state.showSearch = active;
      setStore({
        name: "showSearch",
        content: state.showSearch,
      });
    },
    SET_SHOW_FULL_SCREEN: (state, active) => {
      state.showFullScreen = active;
      setStore({
        name: "showFullScreen",
        content: state.showFullScreen,
      });
    },
    SET_SHOW_DEBUG: (state, active) => {
      state.showDebug = active;
      setStore({
        name: "showDebug",
        content: state.showDebug,
      });
    },
    SET_SHOW_THEME: (state, active) => {
      state.showTheme = active;
      setStore({
        name: "showTheme",
        content: state.showTheme,
      });
    },
    SET_SHOW_COLOR: (state, active) => {
      state.showColor = active;
      setStore({
        name: "showColor",
        content: state.showColor,
      });
    },
    SET_LOCK: (state) => {
      state.isLock = true;
      setStore({
        name: "isLock",
        content: state.isLock,
        type: "session",
      });
    },
    SET_SCREEN: (state, screen) => {
      state.screen = screen;
    },
    SET_THEME: (state, color) => {
      state.theme = color;
      setStore({
        name: "theme",
        content: state.theme,
      });
    },
    SET_THEME_NAME: (state, themeName) => {
      state.themeName = themeName;
      setStore({
        name: "themeName",
        content: state.themeName,
      });
    },
    SET_LOCK_PASSWD: (state, lockPasswd) => {
      state.lockPasswd = lockPasswd;
      setStore({
        name: "lockPasswd",
        content: state.lockPasswd,
        type: "session",
      });
    },
    CLEAR_LOCK: (state) => {
      state.isLock = false;
      state.lockPasswd = "";
      removeStore({
        name: "lockPasswd",
      });
      removeStore({
        name: "isLock",
        type: "session",
      });
    },
    SET_MESSAGE: (state, active) => {
      state.unreadMessage = active;
    },
    SET_PASSWORD_LEVEL: (state, data) => {
      state.passwordLevel = data;
    },
    SET_SHOULD_EDIT: (state, data) => {
      state.shouldEdit = data;
      setStore({ name: "shouldEdit", content: data, type: "session" });
    },
    SET_CHECK_CODE: (state, data) => {
      state.showCheckCode = data;
    },
    SET_CHECK_TIP: (state, data) => {
      state.checkCodeTip = data;
    },
    SET_CALLBACKURL:(state, data) => {
      state.callbackUrl = data;
    }
  },
};
export default common;
